(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTime();
			initClass();
		});
	});
	function initTime(){
		var date = new Date();
		var newDate = getFormatDate(date,"yyyy-MM-dd hh:mm");
		var startTime = getFormatDate(new Date(date.getTime()-1000*60*60*24),"yyyy-MM-dd ");
		$("#start-time").val(startTime+"00:00");
		$("#end-time").val(newDate);
	}
	
	function initClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var data = {
				cmd:"getDataRole",
				model:"license",
				userKey:userKey,
				data:{
					rolecode:userRoles
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var typeOpt = "";
			var gradeOpt = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				for(var i = 0; i < data.length; i++){
					var name = "";
					if(data[i].type == "0"){
						name = "幼儿园";
					}else if(data[i].type == "1"){
						name = "小学";
					}else if(data[i].type == "2"){
						name = "中学";
					}else if(data[i].type == "3"){
						name = "高中";
					}
					typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
				}
				$("#student-type").html(typeOpt);
				
				var gradeData = data[0].data;
				for(var i = 0; i < gradeData.length; i++){
					gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
				}
				$("#student-grade").html(gradeOpt);
				
				var classes = $("#student-grade option:selected").attr("classes").split(",");
				var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
				var classOpt = "";
				for(var j = 0; j < classes.length; j++){
					classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
				}
				$("#student-class").html(classOpt);
				
				$("#student-type").unbind("change");
				$("#student-type").change(function(){
					var gradeData = null;
					var type = $("#student-type option:selected").val();
					var gradeOpt = "";
					if(type != ""){
						for(var i = 0; i < data.length; i++){
							if(data[i].type == type){
								gradeData = data[i].data;
							}
						}
						for(var i = 0; i < gradeData.length; i++){
							gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
						}
					}
					$("#student-grade").html(gradeOpt);
					
					var classOpt = "";
					var classes = $("#student-grade option:selected").attr("classes").split(",");
					var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
					for(var j = 0; j < classes.length; j++){
						classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
					}
					$("#student-class").html(classOpt);
					
				});
				
				$("#student-grade").unbind("change");
				$("#student-grade").change(function(){
					var classes = $("#student-grade option:selected").attr("classes").split(",");
					var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
					var classOpt = "";
					for(var j = 0; j < classes.length; j++){
						if(classcodes[j] == "" && classes[j] == ""){
							classOpt += "<option value='' classes=''>班级</option>";
						}else{
							classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
						}
					}
					$("#student-class").html(classOpt);
					
					getGradeAttence();
				});
				
				$("#student-class").unbind("change");
				$("#student-class").change(function(){
					getGradeAttence();
				});
			}else{
				
			}
			getGradeAttence();
		},null,null);
	}

	function getGradeAttence(){
		processBegin("数据加载中······");
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var type = $("#student-type option:selected").val();
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $("#student-class option:selected").val();
		var grade = $("#student-grade option:selected").val();
		var starttime = strToDate($("#start-time").val() + ":00");
		var endtime = strToDate($("#end-time").val() + ":00");
		var status = $("#st-grade option:selected").val();
		var stname = $("#student-name").val();
		var obj = {};
		obj.starttime = starttime+"";
		obj.endtime = endtime+"";
		obj.type = type;
		obj.rolecode = userRoles;
		obj.classcode = classcode;
		obj.name = stname;
		obj.grade = grade;
		obj.schoolcode = schoolcode;
		if(status == "5"){
			obj.remark = status;
		}else{
			obj.status = status;
		}
//		var obj = {
//				starttime:starttime+"",
//				endtime:endtime+"",
//				type:type,
//				rolecode:userRoles,
//				classcode:classcode,
//				status:status,
//				name:stname,
//				grade:grade,
//				schoolcode:schoolcode
////				currentpage:1,
////				pagesize:4
//		};
		var data = {
				cmd:"queryIntoSchoolRecord",
				model:"attendance",
				userKey:userKey,
				data:obj,
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data.list;
				if(list.length > 0){
					$("#inout-data-show").empty();
					$("#inout-data-show").html(_.template($("#data-template").html(),list));
					processEnd("加载成功");
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#inout-data-show").empty();
					$("#inout-data-show").append(content);
					processEnd("加载成功");
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#inout-data-show").empty();
				$("#inout-data-show").append(content);
				processError("加载失败");
			}
		},null,null);	
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function leadingOutAttences(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var starttime = strToDate($("#start-time").val() + ":00")/1000;
		var endtime = strToDate($("#end-time").val() + ":00")/1000;
		var data = {
			cmd: "getAttendanceByClasscodeByPage",
			model: "attendance",
			userKey: userKey,
			data:{
				starttime: starttime,
				endtime: endtime,
				isdownload: "true",
			},
			remark:"",						
		};
		processBegin("正在导出中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				if(res.remark.flag == true){
					var url = Host + "/downloads/" + res.remark.msg;
					window.frames["hrong"].location = url;
					processEnd("导出成功！");
				}else{
					processError("导出失败！");
				}
			}else{
				processError("暂无数据！");
			}
		},null,null);
	}
	function pagebtns(){
		document.onkeydown = function(e){ 
//			$("input").blur();
		    var ev = document.all ? window.event : e;
		    if(ev.keyCode==13) {
		    	getGradeAttence();
		    }
		};
		$("#attence-search-btn").click(function(){
			now = 0;
//			initLeave(1,pageSize);
			getGradeAttence();
		});
		$(".students-choice",parent.document).unbind("click");
		$(".students-choice",parent.document).click(function(){
			$(".students-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("students-choice-select");
			now = 0;
			getGradeAttence();
//			initLeave(1,pageSize);
			
		});	
		$("#leadingOut").click(function(){
			var content = "";
			var startTime = $("#start-time").val();
			var endTime = $("#end-time").val();
			if(startTime == "" || endTime == ""){
				content += "--  ";
			}else{
				content += startTime+"  -  "+endTime;
			}
			deleteBox("您选择的时间段是：<span class='span-class'>"+content+"</span>，</br>确定要导出吗？",function(){
				leadingOutAttences();
			});
		});
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();